﻿using System.Web.UI;
using Summer.Web.Support;
using Summer.Web.Context;
using System.Web.Mvc;
#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.Web
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009-11-23 10:21:38
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.Web.Mvc
{
    /// <summary>
    /// Extension Methods in System.Web.Mvc.Controller
    /// </summary>
    public static class ControllerContainer
    {
        /// <summary>
        /// Get Object from default context container
        /// The default context container is named as "default" which should be defined in base context
        /// The default context is designed to save the ContextObject about service instance
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetDefaultContextObject(this Controller controller, string key)
        {
            return controller.HttpContext.ApplicationInstance.Application.GetDefaultContextContainer()[key].Object;
        }
        /// <summary>
        /// Get Object from config context container
        /// The config context container is named as "config" which should be defined in base context
        /// The config context is designed to save the ContextObject about page or controller settting
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetConfigContextObject(this Controller controller, string key)
        {
            return controller.HttpContext.ApplicationInstance.Application.GetConfigContextContainer()[key].Object;
        }
        /// <summary>
        /// Get Object from a DIY container
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="contextName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetContextObjectFromContext(this Controller controller, string contextName, string key)
        {
            return controller.HttpContext.ApplicationInstance.Application.GetContextContainerByName(contextName)[key].Object;
        }
        /// <summary>
        /// Get base container
        /// </summary>
        /// <param name="controller"></param>
        /// <returns></returns>
        public static WebContextContainer GetBaseContainer(this Controller controller)
        {
            return (Summer.Web.Context.WebContextContainer)controller.HttpContext.ApplicationInstance.Application.GetContainer();
        }
    }
}
